import { IUser } from '@src/models/User';
import orm from './MockOrm';

async function getOne(email: string): Promise<IUser | null> {
  const [rows] = await orm.pool.execute('SELECT * FROM users WHERE email = ?', [email]);
  const users = rows as IUser[];
  return users.length > 0 ? users[0] : null;
}

async function getAll(): Promise<IUser[]> {
  const [rows] = await orm.pool.execute('SELECT * FROM users');
  return rows as IUser[];
}

async function add(user: IUser): Promise<void> {
  const { name, email } = user;
  await orm.pool.execute(
    'INSERT INTO users (name, email, created) VALUES (?, ?, NOW())',[name, email],
  );
}

export default {
  getOne,
  getAll,
  add,
} as const;